package edu.cmu.vlis.datamining.core;

import java.util.HashSet;
import java.util.Set;

import org.jgrapht.alg.NeighborIndex;
import org.jgrapht.graph.Multigraph;

public class GraphPostProcessor {
    
    Multigraph<Vertex, Edge> _graph;
    NeighborIndex<Vertex,Edge> nIndex;
    
    public GraphPostProcessor(Multigraph<Vertex, Edge> graph) {
        
        this._graph = graph;
        this.nIndex = null;
    }
    
    public NeighborIndex<Vertex,Edge> getNeighborIndex(){
    
        if(null != this.nIndex)
            return this.nIndex;
        
        this.nIndex = new NeighborIndex<Vertex, Edge>(this._graph); 
        return this.nIndex;
    }
    
    public Multigraph<Vertex, Edge> removeVerticesWithNoEdges(){
     
        Set<Vertex> isolatedVertex = new HashSet<Vertex>();
        
        for(Vertex v : this._graph.vertexSet()){
            if(this.nIndex.neighborsOf(v).size() == 0)
                isolatedVertex.add(v);
        }
        System.out.println("Number of vertices before removing isolated vertices: "+this._graph.vertexSet().size());
        this._graph.removeAllVertices(isolatedVertex);
        System.out.println("Number of vertices after removing isolated vertices: "+this._graph.vertexSet().size());
        return this._graph;
    }

}
